package com.example.projects;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.facebook.android.Facebook;

public class CPT3004Menu extends FragmentActivity {
	// Instance of Facebook Class
	private Facebook fb;
	private SharedPreferences mPrefs;
	private String contents;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cpt_3004_menu);
        // create a listener for login/logout events
        mPrefs = getSharedPreferences("", Context.MODE_PRIVATE);

    	// get facebook
        fb = ((CPT3004)getApplication()).getFacebook();
    	if(fb == null) {
    		// create new facebook
    		fb = new Facebook(((CPT3004)getApplication()).getAppID());
    		
    		// prepare facebook for use
        	String access_token = mPrefs.getString("access_token", null);
        	long expires = mPrefs.getLong("access_expires", 0);
        	
        	// reuse old token if possible
        	if (access_token != null) {
    	    	fb.setAccessToken(access_token);
	        	if(expires != 0) {
	        		fb.setAccessExpires(expires);
	        	}
	        	fb.extendAccessTokenIfNeeded(this, null);
        	}
    		// store facebook
    		((CPT3004)getApplication()).setFacebook(fb);
    	}

    	
    }

    public void onResume() {    
        super.onResume();
        fb.extendAccessTokenIfNeeded(this, null);
        MyFacebook.setUsernameID((CPT3004)getApplication());
    }
        
    public void onStart(){
    	
    	super.onStart();
    	fb.extendAccessTokenIfNeeded(this, null);
    	MyFacebook.setUsernameID((CPT3004)getApplication());
    }
    
    public void login(){
    	String name = ((CPT3004)getApplication()).getFBName();
    	String id = ((CPT3004)getApplication()).getFBID();
    	if(name == null || id == null) {
    		MyFacebook.loginToFacebook(this);
    	}
    }
    
    public void logout(){
		MyFacebook.logoutFromFacebook(getApplicationContext(), (CPT3004)getApplication());
    	Toast.makeText(this, "Logout of Facebook", Toast.LENGTH_SHORT).show();
	}
        
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.cpt_3004_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	System.out.println("id="+item.getItemId());
    	// Handle item
    	switch(item.getItemId()) {
    	case R.id.fbbutton:
    		// run mainactivity.java
        	if(fb.isSessionValid()) {
        		logout();
        	}else{
        		login();
        	}
        	
    		return true;
    	case R.id.scanbutton:
    		// run secondactivity.java
    		//String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
    	    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    	    //using scan mode and be able to scan product mode
    	    intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
    	    //start activity and go to zxing api goes back to onActivityResult later.
    	    startActivityForResult(intent, 1);   
    	    return true;
    	    
    	case R.id.editbutton:
    		Intent intent2 = new Intent(this, Edit.class);
    		startActivity(intent2);
    		System.out.println("third");
    		return true;
    		
    	default:
    		return super.onOptionsItemSelected(item);
    	}
    }
    
	//come back from the zxing 
	@Override    
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		
 		if (requestCode == 0) {
	        if (resultCode == RESULT_OK) {
	        	//result after the scan (barcode)
	            contents = intent.getStringExtra("SCAN_RESULT");
	            //String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

	            AccessDatabase ad = new AccessDatabase(this);
	            ad.execute(contents, getFilesDir().getAbsolutePath(), "main");

	        }       
		}
	}
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    	if(fb.isSessionValid()) {
    		menu.findItem(R.id.fbbutton).setTitle("Logout of Facebook");
    	}else{
    		menu.findItem(R.id.fbbutton).setTitle("Login via Facebook");
    	}
    	return true;
    }
}
